{{template "header.html" .}}
<div id="form-result-message">
    <div class="message success hide">文章已更新</div>
    <div class="message error hide">文章更新失败：<span></span></div>
</div>
<form class="form" id="article-edit-form" action="/admin/articles/edit/{{.Article.id}}/" method="post">
    <div id="article-edit-title-item">
        <div class="container relative">
            <input id="article-edit-title" class="ipt" type="text" name="title" placeholder="文章标题" required="required"
                   value="{{.Article.title}}"/>
            <button class="btn save absolute submit" type="submit" id="article-edit-submit">保存文章</button>
            <button class="btn delete absolute submit" name="delete" id="article-edit-delete" type="button">删除文章
            </button>
        </div>
    </div>
    <div id="article-edit-content-item">
        <div class="container">
            <textarea name="content" id="article-edit-content" placeholder="放开写吧！" autofocus="autofocus">{{.Article.content}}</textarea>
        </div>
    </div>
    <div id="article-meta-item" class="item container">
        <p class="item">
            <label for="article-edit-slug">缩略名</label>
            <input class="ipt" id="article-edit-slug" type="text" required="required" name="slug"
                   value="{{.Article.slug}}"/>
            <span class="r"></span>
            <label for="article-edit-category">分类</label>
            <select class="select" name="category" id="article-edit-category" data-index="{{.Article.category}}">
                <option value="-1">未分类</option>
                {{range .Categories}}
                <option value="{{.id}}">{{.title}}</option>
                {{end}}
            </select><span class="r"></span>
            <label for="article-edit-status">状态</label>
            <select class="select" name="status" id="article-edit-status" data-index="{{.Article.status}}">
                <option value="published">发布</option>
                <option value="archived">存档</option>
                <option value="draft">草稿</option>
            </select>
        </p>
        <div class="item clear">
            <label for="article-edit-desc" class="left">描述</label>
            <textarea class="ipt left" name="desc" id="article-edit-desc" rows="10">{{.Article.desc}}</textarea>
            <input type="hidden" id="article-edit-id" value="{{.Article.id}}"/>
        </div>
    </div>
</form>
<script type="text/javascript" src="/admin/js/bootstrap-markdown.js"></script>
<script type="text/javascript" src="/admin/js/marked.js"></script>
<script type="text/javascript" src="/admin/js/jquery.autosize.min.js"></script>
<script type="text/javascript" src="/admin/js/prettify/prettify.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("<link>").attr({ rel: "stylesheet",
            type: "text/css",
            href: "/admin/css/editor.css"
        }).appendTo("head");
        $("<link>").attr({ rel: "stylesheet",
            type: "text/css",
            href: "/admin/js/prettify/prettify.css"
        }).appendTo("head");
        var $content = $('#article-edit-content');
        $content.markdown({
            onPreview: function (e) {
                var content = marked(e.getContent(), {
                    breaks: true
                });
                if (content.indexOf("</pre>") >= 1) {
                    setTimeout(function () {
                        $(".markdown pre").addClass("prettyprint").addClass("linenums");
                        prettyPrint();
                    }, 200);
                }
                return content;
            }
        });
        $content.autosize({append: "\n"});
        var $status = $("#article-edit-status");
        $status.val($status.data("index"));
        var $category = $("#article-edit-category");
        $category.val($category.data("index"));
        ajaxForm("#article-edit-form");
        $('#article-edit-delete').on("click", function () {
            if (confirm("确认删除?")) {
                ajaxRequest("#article-edit-form", "/admin/articles/delete/" + $('#article-edit-id').val() + "/", "DELETE", {}, function (json) {
                    if (json.res) {
                        setTimeout(function () {
                            window.location.href = "/admin/articles/";
                        }, 1000);
                    }
                });
            }
        });
    });
</script>
</body>
</html>